/**
 * Project Name:emcs
 * File Name:ConfigurationManagementController.java
 * Package Name:com.zeei.dsas.controller
 * Date:2016年9月28日下午3:40:48
 * Copyright (c) 2016, ZEEI All Rights Reserved. *
 *
 * ClassName:ConfigurationManagementController
 * Function: TODO ADD FUNCTION.
 * @author   chq
 * @since    JDK 1.8
 */

package com.zeei.ems.base.controller;

import java.util.ArrayList;

import java.util.List;

import javax.annotation.Resource;

import com.github.pagehelper.PageInfo;
import com.zeei.ems.base.result.ResultBasicVO;
import com.zeei.ems.base.result.ResultRowsVO;
import com.zeei.ems.base.service.ConfigurationManagementService;
import com.zeei.ems.base.tree.TreeNodeVO;
import com.zeei.ems.base.util.HttpServiceUtil;
import com.zeei.ems.base.vo.GeneralCategoryVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

@Api(value = "数据字典controller", tags = { "数据字典接口" })
@Controller
@RequestMapping(value = "/ems/dictionary")
public class ConfigurationManagementController extends BasicController {
	@Resource
	private ConfigurationManagementService configurationManagementService;


	/**
	 *
	 * getDictionaryMessage:查询字典信息
	 *
	 * @param generalCategoryParam
	 * @return ResultRowsVO<GeneralCategory>
	 */
	@ApiOperation(value = "查询字典信息", notes = "查询字典信息", httpMethod = "POST")
	@RequestMapping(value = "/info/get", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<GeneralCategoryVO> getDictionaryMessage(GeneralCategoryVO generalCategoryParam) {
		PageInfo<GeneralCategoryVO> info = configurationManagementService.dictionaryManagement(generalCategoryParam);

		return this.createResultRowsVO(info, true);
	}

	/**
	 *
	 * dictionaryEdit:修改字典信息
	 *
	 * @param generalCategoryParam
	 * @param oldGeneralCode
	 * @return ResultBasicVO
	 */
	@ApiOperation(value = "修改字典信息", notes = "修改字典信息", httpMethod = "POST")
	@RequestMapping(value = "/info/update", method = RequestMethod.POST)
	@ResponseBody
	public ResultBasicVO dictionaryEdit(GeneralCategoryVO generalCategoryParam, String oldGeneralCode) {
		boolean flag = false;
		if (HttpServiceUtil.isAccessUrl(getSessionId(), request)) {
			flag = configurationManagementService.editDictionary(generalCategoryParam, oldGeneralCode);

		}
		String message = "修改成功";
		if (!flag) {
			message = "修改失败";
		}
		return this.createResultBasicVO(flag, message);
	}

	/**
	 *
	 * dictionaryAdd:新增字典信息
	 *
	 * @param generalCategoryParam
	 * @return ResultBasicVO
	 */
	@ApiOperation(value = "新增字典信息", notes = "新增字典信息", httpMethod = "POST")
	@RequestMapping(value = "/info/add", method = RequestMethod.POST)
	@ResponseBody
	public ResultBasicVO dictionaryAdd(GeneralCategoryVO generalCategoryParam) {
		boolean flag = configurationManagementService.validGeneralCode(generalCategoryParam.getGeneralCode());
		if(!flag){
			return this.createResultBasicVO(flag, "字典编码已存在!");
		}

		String message = generalCategoryParam.getGeneralCode();
		if (!flag) {
			message = "新增失败";
		}
		return this.createResultBasicVO(flag, message);
	}

	/**
	 *
	 * dictionaryTypeAdd:新增字典类型信息
	 *
	 * @param generalCategoryParam
	 * @return ResultBasicVO
	 */
	@ApiOperation(value = "新增字典类型信息", notes = "新增字典类型信息", httpMethod = "POST")
	@RequestMapping(value = "/category/add", method = RequestMethod.POST)
	@ResponseBody
	public ResultBasicVO dictionaryTypeAdd(GeneralCategoryVO generalCategoryParam) {
		boolean flag = configurationManagementService.validCategoryName(generalCategoryParam.getCategoryName());
		if(!flag){
			return this.createResultBasicVO(flag, "字典名称已存在!");
		}
		flag = configurationManagementService.validCategoryClass(generalCategoryParam.getCategoryClass());
		if(!flag){
			return this.createResultBasicVO(flag, "字典类型已存在!");
		}

		return this.createResultBasicVO(flag);
	}

	/**
	 *
	 * dictionaryDrop:删除字典信息
	 *
	 * @param generalCodes
	 * @return ResultBasicVO
	 */
	@ApiOperation(value = "删除字典信息", notes = "删除字典信息", httpMethod = "POST")
	@RequestMapping(value = "/info/delete", method = RequestMethod.POST)
	@ResponseBody
	public ResultBasicVO dictionaryDrop(String[] generalCodes) {
		boolean flag = false;

		return this.createResultBasicVO(flag);
	}

	/**
	 *
	 * getCategoryList:查询字典信息列表
	 *
	 * @param categoryclass 字典类别
	 * @param type
	 * @return List<GeneralCategory>
	 */
	@ApiOperation(value = "查询字典信息列表", notes = "查询字典信息列表", httpMethod = "POST")
	@RequestMapping(value = "/info/list", method = RequestMethod.POST)
	@ResponseBody
	public List<GeneralCategoryVO> getCategoryList(String categoryclass, Integer type, Integer systemType) {
		List<GeneralCategoryVO> resultList = new ArrayList<GeneralCategoryVO>();
		if (type == 0) {
			GeneralCategoryVO info = new GeneralCategoryVO();
			info.setCategoryClass("");
			info.setCategoryName("全部");
			resultList.add(info);
		}
		List<GeneralCategoryVO> list = configurationManagementService.getCategoryList(categoryclass,systemType,type);
	      for(GeneralCategoryVO generalCategoryVO: list){
	            if("noticeType1".equals(generalCategoryVO.getId())){
	                generalCategoryVO.setId("1");
	            }
	            if("noticeType2".equals(generalCategoryVO.getId())){
	                generalCategoryVO.setId("2");
	            }
	            if("noticeType3".equals(generalCategoryVO.getId())){
	                generalCategoryVO.setId("3");  
	            }
	            if("noticeType4".equals(generalCategoryVO.getId())){
	                generalCategoryVO.setId("4");
	            }
	            if("noticeType5".equals(generalCategoryVO.getId())){
	                generalCategoryVO.setId("5");
	            }
	        }
		resultList.addAll(list);
		return resultList;
	}
	
	/**
	 *
	 * getCategoryClass:查询字典类型列表
	 *
	 * @param type
	 * @return List<GeneralCategory>
	 */
	@ApiOperation(value = "查询字典类型列表", notes = "查询字典类型列表", httpMethod = "POST")
	@RequestMapping(value = "/category/list", method = RequestMethod.POST)
	@ResponseBody
	public List<GeneralCategoryVO> getCategoryClass(int type) {
		List<GeneralCategoryVO> resultList = new ArrayList<>();
		if (type == 0) {
			GeneralCategoryVO info = new GeneralCategoryVO();
			info.setCategoryClass("");
			info.setCategoryName("全部");
			resultList.add(info);
		}
		List<GeneralCategoryVO> list = configurationManagementService.getCategoryClass();
		resultList.addAll(list);
		return resultList;
	}

	/**
	 *
	 * getCategoryTreeList:查询字典类型，返回树形结构
	 *
	 * @param categoryclass
	 * @param type
	 * @return ResultRowsVO<TreeNodeVO<GeneralCategory>>
	 */
	@ApiOperation(value = "查询字典类型，返回树形结构", notes = "查询字典类型，返回树形结构", httpMethod = "POST")
	@RequestMapping(value = "/info/tree", method = RequestMethod.POST)
	@ResponseBody
	public ResultRowsVO<TreeNodeVO<GeneralCategoryVO>> getCategoryTreeList(String categoryclass, Integer systemType, int type) {
		List<GeneralCategoryVO> resultList = new ArrayList<GeneralCategoryVO>();
		if (type == 0) {
			GeneralCategoryVO info = new GeneralCategoryVO();
			info.setCategoryClass("");
			info.setCategoryName("全部");
			resultList.add(info);
		}
		List<GeneralCategoryVO> list = configurationManagementService.getCategoryList(categoryclass, systemType,type);
		List<TreeNodeVO<GeneralCategoryVO>> nodeDataList = new ArrayList<>();
		initGeneralCategoryTree(nodeDataList, list);
		resultList.addAll(list);

		return this.createResultRowsVO(nodeDataList, true, nodeDataList.size());
	}
	
	private void initGeneralCategoryTree(List<TreeNodeVO<GeneralCategoryVO>> nodeDataList, List<GeneralCategoryVO> list) {
		if (list != null && !list.isEmpty()) {
			for (int i = 0; i < list.size(); i++) {
				GeneralCategoryVO info = list.get(i);
				TreeNodeVO<GeneralCategoryVO> nodeDataItem = new TreeNodeVO<GeneralCategoryVO>();
				nodeDataItem.setId(info.getGeneralCode());
				nodeDataItem.setText(info.getCategoryName());
				nodeDataList.add(nodeDataItem);
			}
		}
	}
}
